package com.aaa.security;


import com.aaa.entity.SecurityUser;
import com.aaa.service.FuserServiceDao;
import com.aaa.service.UserServiceDao;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Component
public class JwtEnhancer implements TokenEnhancer {
    @Resource
    FuserServiceDao fuserServiceDao;//前台
    @Resource
    UserServiceDao userServiceDao;//后台
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        String username = oAuth2Authentication.getName();
        System.out.println(username+"113");
        Map<String, Object> map = new HashMap<>();
        SecurityUser securityUser = fuserServiceDao.loadUserByUsername(username);
        SecurityUser securityUser1 = userServiceDao.loadUserByUsername(username);
        if (securityUser!=null){
            map.put("uid",securityUser.getUid());// 查询获取用户id
        }
        if (securityUser1!=null){
            map.put("uid",securityUser1.getUid());// 查询获取用户id
            map.put("username",securityUser1.getUsername());//获取用户的姓名
        }
        DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken)oAuth2AccessToken;
        defaultOAuth2AccessToken.setAdditionalInformation(map);
        return defaultOAuth2AccessToken;
    }
}
